#!/usr/bin/env python
# -*- coding: utf-8 -*-

from datetime import datetime
from persistent import Persistent
from persistent.list import PersistentList
from web_server.models.test_case import TestCase

class Project(Persistent):
    """项目模型"""
    
    def __init__(self, owner: str, name: str, type: str, board: str, product: str, platform: str, version: str, description: str):
        """初始化项目
        
        Args:
            owner (str): 项目所有者
            name (str): 项目名称
            type (str): 项目类型
            board (str): 项目所属板卡
            product (str): 项目所属产品
            platform (str): 项目所属平台
            version (str): 项目版本
            description (str): 项目描述
        """
        super().__init__()
        self.owner = owner
        self.name = name
        self.path = owner + "/" + name
        self.type = type
        self.board = board
        self.product = product
        self.platform = platform
        self.version = version
        self.description = description
        
        # 时间戳
        self.created = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.updated = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.published = None

        self.test_cases = PersistentList()
        self.test_tasks = PersistentList()
        
        # 反向映射：有权限访问此项目的实体列表
        self.authorized_users = PersistentList()    # 用户名列表
        self.authorized_roles = PersistentList()    # 角色名列表  
        self.authorized_groups = PersistentList()   # 用户组名列表
    
    def new_test_case(self, name, description):
        """新建测试用例

        Args:
            name (str): 测试用例名称
            description (str): 测试用例描述
        """
        test_case = TestCase(name, description)
        self.test_cases.append(test_case)

    def get_test_case(self, test_name):
        """获取测试用例
        Args:
            name (str): 测试用例名称
        """
        for test_case in self.test_cases:
            if test_case.name == test_name:
                return test_case
        return None

    def to_dict(self, include_files=False, include_resources=False):
        """转换为字典格式"""
        return {
            'path': self.path,
            'name': self.name,
            'owner': self.owner,
            'type': self.type,
            'description': self.description,
            'board': self.board,
            'product': self.product,
            'platform': self.platform,
            'version': self.version,
            'created': self.created,
            'updated': self.updated,
            'published': self.published,
        }